import {
	Message,
	MessageBox,
	Loading
} from 'element-ui'
import router from '../router'
import store from '../store'

function message(opt = {}) {
	let option = Object.assign({message: '', type: 'success', duration:1000}, opt)
	return Message(option)
}

function messageSuccess(message) {
	return Message({message, type: 'success', duration:3000})
}

function messageError(message) {
	return Message({message, type: 'error', duration:3000})
}

function messageWarning(message) {
	return Message({message, type: 'warning', duration:3000})
}

function confirmDialog(message = '您确定吗？') {
	return new Promise((resolve, reject) => {
		MessageBox.confirm(message, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				dangerouslyUseHTMLString: true,
				closeOnClickModal: false
			})
			.then(() => {
				resolve(true)
			})
			.catch(() => {
				resolve(false)
			})
	})
}

function showLoading(opt = {}){
	let option = Object.assign({text: '加载中', lock: true, background:'rgba(255, 255, 255, 0.5)',body:false}, opt)
	return Loading.service({
		lock: option.lock,
		text: option.text,
		spinner: 'el-icon-loading',
		background: option.background
	});
}

function savePageData(data, key='PageData'){
	return localStorage.setItem(key, JSON.stringify(data));
}

function getPageData(key='PageData'){
	let data = JSON.parse(localStorage.getItem(key))
	localStorage.removeItem(key);
	return data;
}

function removePageData(key='PageData'){
	return localStorage.removeItem(key);
}

function getPageParams(){
	return store.getters.pageParamsMap.get(router.history.current.query.uuid || '') || {};
}

export default {
	message,
	messageSuccess,
	messageError,
	messageWarning,
	confirmDialog,
	showLoading,
	savePageData,
	getPageData,
	removePageData,
	getPageParams
}
